/*
 * CustomerView.java
 *
 * Created on February 21, 2007, 2:53 PM
 * @author Jeff Jenkins
 *
 * @summary: this user interface loads a existing customer or a black customer 
 * object into the GUI. It then provides a button to edit the customer
 * informatoin. If you push the button all the fields are endabled and the 
 * done button turns into a save button. When the save button is pushed 
 * the information if changed is saved to the database. If a membership does
 * not exist one is created when membership information is entered.
 *
 */

package edu.byu.isys413.jjenkins.view;
import edu.byu.isys413.jjenkins.data.*;
import java.sql.Date;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;



/**
 *
 * @author  Jeff
 *
 * @summary: this user interface loads a existing customer or a black customer 
 * object into the GUI. It then provides a button to edit the customer
 * informatoin. If you push the button all the fields are endabled and the 
 * done button turns into a save button. When the save button is pushed 
 * the information if changed is saved to the database. If a membership does
 * not exist one is created when membership information is entered.
 */
public class CustomerView extends javax.swing.JFrame {
    
    // List to hold check box
    private List InterestPool = new LinkedList();
    
    // List of Interest in Database
    // I need to access this in serveral methods
    private List li;
    
    CustomerBO customer;
    
    private static CustomerView instance = null;
    
    /** Creates new form CustomerView
     * I'm implementing teh singleton pattern so that only one instance of this
     * Gui will be created*/
    private CustomerView() {
        initComponents();
        this.pack();
        
        
        
        
        // This enables and disables the field
        btnDoneSave.requestFocus();
        
        //this method populates the interest panel
        populateInterests();
        
    }
    
    /* This method insures that only one instance of CustomerView is created
        @return CustomerView
     */
    
    public static synchronized CustomerView getInstance() {
        
        if (instance == null) {
            instance = new CustomerView();
        }
        
        
        
        return instance;
    }
    
    /* This method opens the screen for a new customer. A customer object
     * must be created must be created to run this method.
     *
     * @par String id (for getting the customer)
     */
    
    public void loadCustomer(CustomerBO customer, Boolean edit){
        
        //Sets the focus to the done button
        this.customer=customer;
        
        //Enables or disables text fields
        if(edit){
            setToEdit();
        }else{
            setToRead();
        }
        
        txtCity.setText(customer.getCity());
        txtEmail.setText(customer.getEmail());
        txtFirstName.setText(customer.getFirstName());
        txtLastName.setText(customer.getLastName());
        txtPhone.setText(customer.getPhone());
        txtState.setText(customer.getState());
        txtZip.setText(customer.getZip());
        txtStreet.setText(customer.getAddress());
        
        
        if(customer.getMember() != null){
            txtCreditCard.setText(String.valueOf(customer.getMember().getCreditCard()));
            txtExpireDate.setText(String.valueOf(customer.getMember().getExpireDate()));
            txtStartDate.setText(String.valueOf(customer.getMember().getStartDate()));
            
            //sets the customer's areas of interest
            setCustomerInterests(customer.getMember().getAreasOfInterest());
        }else{
            txtCreditCard.setText("");
            txtExpireDate.setText("yyyy-mm-dd");
            txtStartDate.setText("yyyy-mm-dd");
            resetCustomerInterests();
            
        }
        
        setVisible(true);
    }
    /*This method unchecks all the interests
     *
     */
    
    public void resetCustomerInterests(){
        for(int i = 0; i< InterestPool.size(); i++){
            ((javax.swing.JCheckBox)InterestPool.get(i)).setSelected(false);
            
        }
    }
    
    
    /*This method set the cusotmers areas of interest. A list of customer interest
     *are passed in and it will check or uncheck the interests
     *
     *@par: List interest
     */
    public void setCustomerInterests(List memLi){
        
        for(int i = 0; i< InterestPool.size(); i++){
            if(memLi.contains(((InterestBO)li.get(i)).getId())){
                ((javax.swing.JCheckBox)InterestPool.get(i)).setSelected(true);
                
            }else{
                ((javax.swing.JCheckBox)InterestPool.get(i)).setSelected(false);
            }
        }
    }
    
    /* This method gets a list of all interests, then marks what interests the
     * member has already made
     */
    
    public void populateInterests(){
        try{
            
            
            // List of all interesets
            li = InterestDAO.getInstance().getAll();
            
            // used to determine whether to put in right or left column
            boolean left = true;
            
            // creaetes checkboxes
            for(int i = 0; i < li.size(); i++){
                InterestPool.add(new javax.swing.JCheckBox());
                ((javax.swing.JCheckBox)InterestPool.get(i)).setText(((InterestBO)li.get(i)).getDescription());
                ((javax.swing.JCheckBox)InterestPool.get(i)).setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
                ((javax.swing.JCheckBox)InterestPool.get(i)).setMargin(new java.awt.Insets(0, 0, 0, 0));
                
                ((javax.swing.JCheckBox)InterestPool.get(i)).addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        CheckChanged(evt);
                    }
                });
                
                if(left){
                    pnlLeft.add((javax.swing.JCheckBox)InterestPool.get(i));
                    left = false;
                }else{
                    pnlRight.add((javax.swing.JCheckBox)InterestPool.get(i));
                    left = true;
                }
                
                
            }
            
            
            
        }catch(DataException ex){
            JOptionPane.showMessageDialog(null,ex.toString(),"Error add Areas of Interset", JOptionPane.ERROR_MESSAGE);
        }catch(IndexOutOfBoundsException ex){
            JOptionPane.showMessageDialog(null,ex.toString(),"Error add Areas of Interset", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    // This method enables all text field and changes the button text
    public void setToEdit(){
        // Sets Text of button
        
        btnEditCancel.setText("Cancel");
        btnEditCancel.setEnabled(false);
        btnDoneSave.setText("Save");
        
        
        // Sets text to Editable
        txtCity.setEditable(true);
        txtCreditCard.setEditable(true);
        txtEmail.setEditable(true);
        txtExpireDate.setEditable(true);
        txtFirstName.setEditable(true);
        txtLastName.setEditable(true);
        txtPhone.setEditable(true);
        txtStartDate.setEditable(true);
        txtState.setEditable(true);
        txtStreet.setEditable(true);
        txtZip.setEditable(true);
        
        for(int i = 0; i < InterestPool.size(); i++){
            ((javax.swing.JCheckBox)InterestPool.get(i)).setEnabled(true);
        }
        
    }
    
    // This method disables all text fields and changes the button's text
    public void setToRead(){
        // Sets Text of button
        
        btnEditCancel.setText("Edit");
        btnDoneSave.setText("Done");
        btnEditCancel.setEnabled(true);
        
        
        // Sets text to Editable
        txtCity.setEditable(false);
        txtCreditCard.setEditable(false);
        txtEmail.setEditable(false);
        txtExpireDate.setEditable(false);
        txtFirstName.setEditable(false);
        txtLastName.setEditable(false);
        txtPhone.setEditable(false);
        txtStartDate.setEditable(false);
        txtState.setEditable(false);
        txtStreet.setEditable(false);
        txtZip.setEditable(false);
        
        for(int i = 0; i < InterestPool.size(); i++){
            ((javax.swing.JCheckBox)InterestPool.get(i)).setEnabled(false);
        }
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel2 = new javax.swing.JPanel();
        jPanel46 = new javax.swing.JPanel();
        jLabel15 = new javax.swing.JLabel();
        jPanel47 = new javax.swing.JPanel();
        jPanel48 = new javax.swing.JPanel();
        jPanel49 = new javax.swing.JPanel();
        jLabel16 = new javax.swing.JLabel();
        jPanel50 = new javax.swing.JPanel();
        txtFirstName = new javax.swing.JTextField();
        jPanel51 = new javax.swing.JPanel();
        jPanel52 = new javax.swing.JPanel();
        jLabel17 = new javax.swing.JLabel();
        jPanel53 = new javax.swing.JPanel();
        txtLastName = new javax.swing.JTextField();
        jPanel54 = new javax.swing.JPanel();
        jPanel55 = new javax.swing.JPanel();
        jPanel56 = new javax.swing.JPanel();
        jLabel18 = new javax.swing.JLabel();
        jPanel57 = new javax.swing.JPanel();
        txtEmail = new javax.swing.JTextField();
        jPanel58 = new javax.swing.JPanel();
        jPanel59 = new javax.swing.JPanel();
        jLabel19 = new javax.swing.JLabel();
        jPanel60 = new javax.swing.JPanel();
        txtPhone = new javax.swing.JTextField();
        jPanel61 = new javax.swing.JPanel();
        jPanel62 = new javax.swing.JPanel();
        jPanel63 = new javax.swing.JPanel();
        jLabel20 = new javax.swing.JLabel();
        jPanel64 = new javax.swing.JPanel();
        txtStreet = new javax.swing.JTextField();
        jPanel65 = new javax.swing.JPanel();
        jPanel66 = new javax.swing.JPanel();
        jLabel21 = new javax.swing.JLabel();
        jPanel67 = new javax.swing.JPanel();
        txtCity = new javax.swing.JTextField();
        jPanel68 = new javax.swing.JPanel();
        jPanel69 = new javax.swing.JPanel();
        jPanel70 = new javax.swing.JPanel();
        jLabel22 = new javax.swing.JLabel();
        jPanel71 = new javax.swing.JPanel();
        txtState = new javax.swing.JTextField();
        jPanel72 = new javax.swing.JPanel();
        jPanel73 = new javax.swing.JPanel();
        jLabel23 = new javax.swing.JLabel();
        jPanel74 = new javax.swing.JPanel();
        txtZip = new javax.swing.JTextField();
        jPanel75 = new javax.swing.JPanel();
        jLabel24 = new javax.swing.JLabel();
        jPanel76 = new javax.swing.JPanel();
        jPanel77 = new javax.swing.JPanel();
        jPanel78 = new javax.swing.JPanel();
        jLabel25 = new javax.swing.JLabel();
        jPanel79 = new javax.swing.JPanel();
        txtCreditCard = new javax.swing.JTextField();
        jPanel80 = new javax.swing.JPanel();
        jPanel81 = new javax.swing.JPanel();
        jLabel26 = new javax.swing.JLabel();
        jPanel82 = new javax.swing.JPanel();
        txtExpireDate = new javax.swing.JTextField();
        jPanel83 = new javax.swing.JPanel();
        jPanel84 = new javax.swing.JPanel();
        jPanel85 = new javax.swing.JPanel();
        jLabel27 = new javax.swing.JLabel();
        jPanel86 = new javax.swing.JPanel();
        txtStartDate = new javax.swing.JTextField();
        jPanel87 = new javax.swing.JPanel();
        jPanel117 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel14 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jScrollBar1 = new javax.swing.JScrollBar();
        pnlInterests = new javax.swing.JPanel();
        pnlRight = new javax.swing.JPanel();
        pnlLeft = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        btnEditCancel = new javax.swing.JButton();
        btnDoneSave = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.BorderLayout(10, 10));

        setTitle("Customer Information");
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setResizable(false);
        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 1, 1));
        jPanel2.setRequestFocusEnabled(false);
        jPanel46.setLayout(new java.awt.GridLayout(5, 1));

        jPanel46.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 1, 1));
        jPanel46.setOpaque(false);
        jPanel46.setPreferredSize(new java.awt.Dimension(500, 150));
        jPanel46.setVerifyInputWhenFocusTarget(false);
        jLabel15.setText("Customer:");
        jLabel15.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel46.add(jLabel15);

        jPanel47.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

        jPanel47.setPreferredSize(new java.awt.Dimension(0, 0));
        jPanel48.setLayout(new java.awt.GridLayout(1, 0));

        jPanel48.setAutoscrolls(true);
        jPanel48.setPreferredSize(new java.awt.Dimension(100, 100));
        jPanel49.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jPanel49.setPreferredSize(new java.awt.Dimension(500, 500));
        jLabel16.setText("First Name:");
        jPanel49.add(jLabel16);

        jPanel48.add(jPanel49);

        jPanel50.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        txtFirstName.setPreferredSize(new java.awt.Dimension(100, 20));
        txtFirstName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtFirstNameActionPerformed(evt);
            }
        });
        txtFirstName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtFirstNameFocusLost(evt);
            }
        });

        jPanel50.add(txtFirstName);

        jPanel48.add(jPanel50);

        jPanel47.add(jPanel48);

        jPanel51.setLayout(new java.awt.GridLayout(1, 2));

        jPanel52.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel17.setText("Last Name:");
        jPanel52.add(jLabel17);

        jPanel51.add(jPanel52);

        jPanel53.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        txtLastName.setPreferredSize(new java.awt.Dimension(100, 20));
        txtLastName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtLastNameFocusLost(evt);
            }
        });

        jPanel53.add(txtLastName);

        jPanel51.add(jPanel53);

        jPanel47.add(jPanel51);

        jPanel46.add(jPanel47);

        jPanel54.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

        jPanel54.setPreferredSize(new java.awt.Dimension(0, 0));
        jPanel55.setLayout(new java.awt.GridLayout(1, 0));

        jPanel55.setAutoscrolls(true);
        jPanel55.setPreferredSize(new java.awt.Dimension(100, 100));
        jPanel56.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel18.setText("Email:");
        jPanel56.add(jLabel18);

        jPanel55.add(jPanel56);

        jPanel57.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        txtEmail.setPreferredSize(new java.awt.Dimension(100, 20));
        txtEmail.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtEmailFocusLost(evt);
            }
        });

        jPanel57.add(txtEmail);

        jPanel55.add(jPanel57);

        jPanel54.add(jPanel55);

        jPanel58.setLayout(new java.awt.GridLayout(1, 2));

        jPanel59.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel19.setText("Phone:");
        jPanel59.add(jLabel19);

        jPanel58.add(jPanel59);

        jPanel60.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        txtPhone.setPreferredSize(new java.awt.Dimension(100, 20));
        txtPhone.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPhoneFocusLost(evt);
            }
        });

        jPanel60.add(txtPhone);

        jPanel58.add(jPanel60);

        jPanel54.add(jPanel58);

        jPanel46.add(jPanel54);

        jPanel61.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

        jPanel61.setPreferredSize(new java.awt.Dimension(0, 0));
        jPanel62.setLayout(new java.awt.GridLayout(1, 0));

        jPanel62.setAutoscrolls(true);
        jPanel62.setPreferredSize(new java.awt.Dimension(100, 100));
        jPanel63.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel20.setText("Street Address:");
        jPanel63.add(jLabel20);

        jPanel62.add(jPanel63);

        jPanel64.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        txtStreet.setPreferredSize(new java.awt.Dimension(100, 20));
        txtStreet.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtStreetFocusLost(evt);
            }
        });

        jPanel64.add(txtStreet);

        jPanel62.add(jPanel64);

        jPanel61.add(jPanel62);

        jPanel65.setLayout(new java.awt.GridLayout(1, 2));

        jPanel66.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel21.setText("City:");
        jPanel66.add(jLabel21);

        jPanel65.add(jPanel66);

        jPanel67.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        txtCity.setPreferredSize(new java.awt.Dimension(100, 20));
        txtCity.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCityFocusLost(evt);
            }
        });

        jPanel67.add(txtCity);

        jPanel65.add(jPanel67);

        jPanel61.add(jPanel65);

        jPanel46.add(jPanel61);

        jPanel68.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

        jPanel68.setPreferredSize(new java.awt.Dimension(0, 0));
        jPanel69.setLayout(new java.awt.GridLayout(1, 0));

        jPanel69.setAutoscrolls(true);
        jPanel69.setPreferredSize(new java.awt.Dimension(100, 100));
        jPanel70.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel22.setText("State:");
        jPanel70.add(jLabel22);

        jPanel69.add(jPanel70);

        jPanel71.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        txtState.setPreferredSize(new java.awt.Dimension(100, 20));
        txtState.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtStateFocusLost(evt);
            }
        });

        jPanel71.add(txtState);

        jPanel69.add(jPanel71);

        jPanel68.add(jPanel69);

        jPanel72.setLayout(new java.awt.GridLayout(1, 2));

        jPanel73.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel23.setText("Zip:");
        jPanel73.add(jLabel23);

        jPanel72.add(jPanel73);

        jPanel74.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        txtZip.setPreferredSize(new java.awt.Dimension(100, 20));
        txtZip.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtZipFocusLost(evt);
            }
        });

        jPanel74.add(txtZip);

        jPanel72.add(jPanel74);

        jPanel68.add(jPanel72);

        jPanel46.add(jPanel68);

        jPanel2.add(jPanel46, java.awt.BorderLayout.CENTER);

        jPanel75.setLayout(new java.awt.GridLayout(3, 0));

        jPanel75.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 1, 1));
        jPanel75.setPreferredSize(new java.awt.Dimension(450, 90));
        jLabel24.setText("Member:");
        jLabel24.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel75.add(jLabel24);

        jPanel76.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

        jPanel76.setPreferredSize(new java.awt.Dimension(0, 0));
        jPanel77.setLayout(new java.awt.GridLayout(1, 0));

        jPanel77.setAutoscrolls(true);
        jPanel77.setPreferredSize(new java.awt.Dimension(100, 100));
        jPanel78.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel25.setText("Credit Card:");
        jPanel78.add(jLabel25);

        jPanel77.add(jPanel78);

        jPanel79.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        txtCreditCard.setPreferredSize(new java.awt.Dimension(100, 20));
        txtCreditCard.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCreditCardFocusLost(evt);
            }
        });

        jPanel79.add(txtCreditCard);

        jPanel77.add(jPanel79);

        jPanel76.add(jPanel77);

        jPanel80.setLayout(new java.awt.GridLayout(1, 2));

        jPanel81.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel26.setText("Expiration Date:");
        jPanel81.add(jLabel26);

        jPanel80.add(jPanel81);

        jPanel82.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        txtExpireDate.setText("yyyy-mm-dd");
        txtExpireDate.setPreferredSize(new java.awt.Dimension(100, 20));
        txtExpireDate.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtExpireDateFocusLost(evt);
            }
        });

        jPanel82.add(txtExpireDate);

        jPanel80.add(jPanel82);

        jPanel76.add(jPanel80);

        jPanel75.add(jPanel76);

        jPanel83.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

        jPanel83.setPreferredSize(new java.awt.Dimension(0, 0));
        jPanel84.setLayout(new java.awt.GridLayout(1, 0));

        jPanel84.setAutoscrolls(true);
        jPanel84.setPreferredSize(new java.awt.Dimension(0, 0));
        jPanel85.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jPanel85.setPreferredSize(new java.awt.Dimension(0, 0));
        jLabel27.setText("Start Date:");
        jPanel85.add(jLabel27);

        jPanel84.add(jPanel85);

        jPanel86.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jPanel86.setPreferredSize(new java.awt.Dimension(0, 0));
        txtStartDate.setText("yyyy-mm-dd");
        txtStartDate.setPreferredSize(new java.awt.Dimension(100, 20));
        txtStartDate.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtStartDateFocusLost(evt);
            }
        });

        jPanel86.add(txtStartDate);

        jPanel84.add(jPanel86);

        jPanel83.add(jPanel84);

        jPanel87.setLayout(new java.awt.GridLayout(1, 2));

        jPanel83.add(jPanel87);

        jPanel75.add(jPanel83);

        jPanel2.add(jPanel75, java.awt.BorderLayout.SOUTH);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        jPanel117.setLayout(new java.awt.BorderLayout());

        jPanel117.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel4.setPreferredSize(new java.awt.Dimension(450, 120));
        jPanel1.setPreferredSize(new java.awt.Dimension(200, 20));
        jLabel14.setText("        Areas of Interest:");
        jPanel1.add(jLabel14);

        jPanel4.add(jPanel1, java.awt.BorderLayout.CENTER);

        jScrollPane1.setPreferredSize(new java.awt.Dimension(300, 100));
        jScrollPane1.setViewportView(jScrollBar1);

        pnlInterests.setLayout(new java.awt.GridLayout(1, 2));

        pnlInterests.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        pnlRight.setLayout(new javax.swing.BoxLayout(pnlRight, javax.swing.BoxLayout.Y_AXIS));

        pnlInterests.add(pnlRight);

        pnlLeft.setLayout(new javax.swing.BoxLayout(pnlLeft, javax.swing.BoxLayout.Y_AXIS));

        pnlInterests.add(pnlLeft);

        jScrollPane1.setViewportView(pnlInterests);

        jPanel4.add(jScrollPane1, java.awt.BorderLayout.SOUTH);

        jPanel117.add(jPanel4, java.awt.BorderLayout.NORTH);

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        jPanel3.setMinimumSize(new java.awt.Dimension(1, 1));
        this.pack();
        jPanel3.setPreferredSize(new java.awt.Dimension(450, 50));
        jPanel3.setRequestFocusEnabled(false);
        jPanel3.setVerifyInputWhenFocusTarget(false);
        btnEditCancel.setText("Edit");
        btnEditCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditCancelActionPerformed(evt);
            }
        });

        jPanel3.add(btnEditCancel);

        btnDoneSave.setText("Done");
        btnDoneSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDoneSaveActionPerformed(evt);
            }
        });

        jPanel3.add(btnDoneSave);

        jPanel117.add(jPanel3, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel117, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // calls setter from object to set new value
    private void txtStartDateFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtStartDateFocusLost
        try{
            
            // checks to see if there is a member if not, on is created
            if(customer.getMember() == null){
                MemberBO member = MembershipDAO.getInstance().create();
                customer.setMember(member);
                member.setCustomer(customer);
            }
            
            if(txtStartDate.getText() != ""){
                String date = txtStartDate.getText();
                String[] components = date.split("-");
                int month = Integer.parseInt(components[1]);
                int day = Integer.parseInt(components[2]);
                int year = Integer.parseInt(components[0]) - 1900;
                System.out.println("year is; " + year);
                
                
                //This has been depreciated. Consider using another alternative. Possibly storing date as string
                customer.getMember().setStartDate(new Date(year, month -1, day));
                
            }
        }catch(Exception e){
            JOptionPane.showMessageDialog(null,"Error creating start date. Please enter yyyy-mm-dd","Error Saving Object", JOptionPane.ERROR_MESSAGE);
            
        }
        
    }//GEN-LAST:event_txtStartDateFocusLost
// calls setter from object to set new value
    private void txtExpireDateFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtExpireDateFocusLost
        try{
            // checks to see if there is a member if not, on is created
            if(customer.getMember() == null){
                MemberBO member = MembershipDAO.getInstance().create();
                customer.setMember(member);
                member.setCustomer(customer);
            }
            
            // gets date
            
            if(txtExpireDate.getText() != ""){
                String date = txtExpireDate.getText();
                String[] components = date.split("-");
                int month = Integer.parseInt(components[1]);
                int day = Integer.parseInt(components[2]);
                int year = Integer.parseInt(components[0]) - 1900;
                
                //This has been depreciated. Consider using another alternative. Possibly storing date as string
                customer.getMember().setExpireDate(new Date(year, month -1, day));
                
            }
        }catch(Exception e){
            JOptionPane.showMessageDialog(null,"Error creating expire date. Please enter date yyyy-mm-dd","Error Saving Object", JOptionPane.ERROR_MESSAGE);
            
        }
    }//GEN-LAST:event_txtExpireDateFocusLost
// calls setter from object to set new value
    private void txtZipFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtZipFocusLost
        customer.setZip(txtZip.getText());
        
    }//GEN-LAST:event_txtZipFocusLost
// calls setter from object to set new value
    private void txtStateFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtStateFocusLost
        customer.setState(txtState.getText());
    }//GEN-LAST:event_txtStateFocusLost
// calls setter from object to set new value
    private void txtCityFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCityFocusLost
        customer.setCity(txtCity.getText());
    }//GEN-LAST:event_txtCityFocusLost
// calls setter from object to set new value
    private void txtStreetFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtStreetFocusLost
        customer.setAddress(txtStreet.getText());
    }//GEN-LAST:event_txtStreetFocusLost
// calls setter from object to set new value
    private void txtPhoneFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPhoneFocusLost
        customer.setPhone(txtPhone.getText());
    }//GEN-LAST:event_txtPhoneFocusLost
// calls setter from object to set new value
    private void txtEmailFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtEmailFocusLost
        customer.setEmail(txtEmail.getText());
    }//GEN-LAST:event_txtEmailFocusLost
// calls setter from object to set new value
    private void txtLastNameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtLastNameFocusLost
        customer.setLastName(txtLastName.getText());
        
    }//GEN-LAST:event_txtLastNameFocusLost
// calls setter from object to set new value
    private void txtCreditCardFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCreditCardFocusLost
        try{
// checks to see if there is a member if not, on is created
            if(customer.getMember() == null){
                MemberBO member = MembershipDAO.getInstance().create();
                customer.setMember(member);
                member.setCustomer(customer);
            }
            
            customer.getMember().setCreditCard(txtCreditCard.getText());
        }catch(DataException ex){
            JOptionPane.showMessageDialog(null,ex.toString(),"Error creating member", JOptionPane.ERROR_MESSAGE);
            
        }
    }//GEN-LAST:event_txtCreditCardFocusLost
// calls setter from object to set new value
    private void txtFirstNameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtFirstNameFocusLost
        customer.setFirstName(txtFirstName.getText());
    }//GEN-LAST:event_txtFirstNameFocusLost
    
    // This method is called when a check box is clicked or unclicked. It is used to
    // to add or delete areas of interest from a member
    private void CheckChanged(java.awt.event.ActionEvent evt){
        
        // finds out if the button was selected or deselected
        if(((javax.swing.JCheckBox)evt.getSource()).isSelected()){
            
            
            // gets index number of object
            int index = InterestPool.indexOf(evt.getSource());
            
            //uses index number to find interest id and add to customer
            
            customer.getMember().setAreaOfInterest(((InterestBO)li.get(index)).getId());
            
            
        }else{
            
            // gets index number of object
            int index = InterestPool.indexOf(evt.getSource());
            
            //uses index number to find interest id and add to customer
            
            customer.getMember().deleteAreaOfInterest(((InterestBO)li.get(index)).getId());
            
        }
        
    }
    
    // This method is called when the save or done button is pushed. The Done button sets
    // the frame to not visible
    // The save button saves the customer
    private void btnDoneSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDoneSaveActionPerformed
        if(btnDoneSave.getText()== "Save"){
            // customer save
            setToRead();
            try {
                
                CustomerDAO.getInstance().save(customer);
                // This enables and disables the field
                btnDoneSave.requestFocus();
                
                
                
            } catch (DataException ex) {
                JOptionPane.showMessageDialog(null,ex.toString(),"Error saving customer", JOptionPane.ERROR_MESSAGE);
                
            }
        }else{
            
       
                SelectCustomer.getInstance().populateList();
                this.setVisible(false);
                
                
                
            
        }
    }//GEN-LAST:event_btnDoneSaveActionPerformed
    
    private void btnEditCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditCancelActionPerformed
        if(btnEditCancel.getText() == "Edit"){
            
            
            setToEdit();
        }else{
            
            setToRead();
            //  Resotore
            // We'll make this later. Right now the button is disabled'
        }
    }//GEN-LAST:event_btnEditCancelActionPerformed
    
    
    
    private void txtFirstNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtFirstNameActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_txtFirstNameActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CustomerView().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDoneSave;
    private javax.swing.JButton btnEditCancel;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel117;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel46;
    private javax.swing.JPanel jPanel47;
    private javax.swing.JPanel jPanel48;
    private javax.swing.JPanel jPanel49;
    private javax.swing.JPanel jPanel50;
    private javax.swing.JPanel jPanel51;
    private javax.swing.JPanel jPanel52;
    private javax.swing.JPanel jPanel53;
    private javax.swing.JPanel jPanel54;
    private javax.swing.JPanel jPanel55;
    private javax.swing.JPanel jPanel56;
    private javax.swing.JPanel jPanel57;
    private javax.swing.JPanel jPanel58;
    private javax.swing.JPanel jPanel59;
    private javax.swing.JPanel jPanel60;
    private javax.swing.JPanel jPanel61;
    private javax.swing.JPanel jPanel62;
    private javax.swing.JPanel jPanel63;
    private javax.swing.JPanel jPanel64;
    private javax.swing.JPanel jPanel65;
    private javax.swing.JPanel jPanel66;
    private javax.swing.JPanel jPanel67;
    private javax.swing.JPanel jPanel68;
    private javax.swing.JPanel jPanel69;
    private javax.swing.JPanel jPanel70;
    private javax.swing.JPanel jPanel71;
    private javax.swing.JPanel jPanel72;
    private javax.swing.JPanel jPanel73;
    private javax.swing.JPanel jPanel74;
    private javax.swing.JPanel jPanel75;
    private javax.swing.JPanel jPanel76;
    private javax.swing.JPanel jPanel77;
    private javax.swing.JPanel jPanel78;
    private javax.swing.JPanel jPanel79;
    private javax.swing.JPanel jPanel80;
    private javax.swing.JPanel jPanel81;
    private javax.swing.JPanel jPanel82;
    private javax.swing.JPanel jPanel83;
    private javax.swing.JPanel jPanel84;
    private javax.swing.JPanel jPanel85;
    private javax.swing.JPanel jPanel86;
    private javax.swing.JPanel jPanel87;
    private javax.swing.JScrollBar jScrollBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnlInterests;
    private javax.swing.JPanel pnlLeft;
    private javax.swing.JPanel pnlRight;
    private javax.swing.JTextField txtCity;
    private javax.swing.JTextField txtCreditCard;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtExpireDate;
    private javax.swing.JTextField txtFirstName;
    private javax.swing.JTextField txtLastName;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtStartDate;
    private javax.swing.JTextField txtState;
    private javax.swing.JTextField txtStreet;
    private javax.swing.JTextField txtZip;
    // End of variables declaration//GEN-END:variables
    
}
